import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';

import '../../resources/kq_theme_colors.dart';
import '../checkbox/kq_checkbox.dart';
import '../divider/kq_divider.dart';

class KqChooseItemView extends StatefulWidget {
  final String? title;
  final bool isChecked;
  final CheckBoxDirection direction;
  final Function(bool)? onItemTap;
  final bool disabled;

  /// 是否build时自动设置_checked=isChecked
  final bool updateCheckedWhenBuild;
  final Color bgColor;
  final bool hideDivider;
  final double? fontSize;

  const KqChooseItemView({
    super.key,
    this.isChecked = false,
    this.title,
    this.onItemTap,
    this.bgColor = KqThemeColors.bgWhite,
    this.direction = CheckBoxDirection.left,
    this.disabled = false,
    this.updateCheckedWhenBuild = false,
    this.hideDivider = false,
    this.fontSize,
  });

  @override
  State<StatefulWidget> createState() => _KqChooseItemViewState();
}

class _KqChooseItemViewState extends State<KqChooseItemView> {
  bool _isChecked = false;

  @override
  void initState() {
    super.initState();
    _isChecked = widget.isChecked;
  }

  @override
  Widget build(BuildContext context) {
    if (widget.updateCheckedWhenBuild) {
      _isChecked = widget.isChecked;
    }
    return InkWell(
      onTap: () {
        if (!widget.disabled) {
          _isChecked = !_isChecked;
          widget.onItemTap?.call(_isChecked);
          setState(() {});
        }
      },
      child: Column(
        children: [
          Container(
            color: widget.bgColor,
            height: 44.r,
            padding: EdgeInsets.symmetric(horizontal: 15.r),
            child: Row(
              children: [
                Visibility(
                    visible: widget.direction == CheckBoxDirection.left,
                    child: IgnorePointer(
                        child: KqCheckBox(
                      value: _isChecked,
                      enable: !widget.disabled,
                    ))),
                Expanded(
                    child: Container(
                        padding: EdgeInsets.only(
                            left: widget.direction == CheckBoxDirection.left
                                ? 12.r
                                : 0),
                        child: Text(
                          widget.title ?? '',
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              fontSize: widget.fontSize ?? 14.sp,
                              color: KqThemeColors.text26),
                        ))),
                Visibility(
                    visible: widget.direction != CheckBoxDirection.left,
                    child: IgnorePointer(
                        child: KqCheckBox(
                      value: _isChecked,
                      enable: !widget.disabled,
                    )))
              ],
            ),
          ),
          Visibility(visible: !widget.hideDivider, child: const KqDivider()),
        ],
      ),
    );
  }
}

enum CheckBoxDirection { left, right }
